/*
 * Copyright 2018 dc-square GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hivemq.extensions.rbac.utils;

import com.hivemq.extension.sdk.api.annotations.NotNull;
import com.hivemq.extension.sdk.api.annotations.Nullable;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.lookup.StringLookup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

/**
 * 变量替换,用户替换 用户变量,如客户端id和用户名等
 * @author MI
 */
public class Substitution {

    private static final Logger log = LoggerFactory.getLogger(Substitution.class);

    /**  词头 */
    private static final String PREFIX = "${{";
    /** 词尾 */
    private static final String SUFFIX = "}}";
    /** 逃逸字符 */
    private static final String ESCAPE_CHAR = "§";

    /**
     * 变量字符替换 <code>${{}}</code>
     * <p>
     * Can replace <code>${{clientid}}</code> and <code>${{username}}</code>
     * <p>
     *
     * @param topic    topic pattern
     * @param clientId actual clientId
     * @param username actual username
     * @return substituted string
     */
    @NotNull
    public static String substitute(@NotNull final String topic, @NotNull final String clientId, @NotNull final String username) {

        final StringSubstitutor strSubstitutor = new StringSubstitutor(new StringLookup() {
            @Nullable
            @Override
            public String lookup(@NotNull final String key) {
                final String lowerCaseKey = key.toLowerCase();

                // 替换clientid和username
                if ("clientid".equals(lowerCaseKey)) {
                    return clientId;
                } else if ("username".equals(lowerCaseKey)) {
                    return username;
                }

                return null;
            }
        }, PREFIX, SUFFIX, ESCAPE_CHAR.charAt(0));

        return strSubstitutor.replace(topic);
    }

    /**
     * ByteBuffer 转换 String
     * @param buffer
     * @return
     */
    public static String getString(ByteBuffer buffer)
    {
        Charset charset = null;
        CharsetDecoder decoder = null;
        CharBuffer charBuffer = null;
        try
        {
            charset = Charset.forName("UTF-8");
            decoder = charset.newDecoder();
            // charBuffer = decoder.decode(buffer);//用这个的话，只能输出来一次结果，第二次显示为空
            charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
            return charBuffer.toString();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return "";
        }
    }
}
